<?php

/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file user.class.php
 * @author deltamaster
 * @date 2011-4-11
 * @description The class for user authentication.
 */

if (!defined('IN_IMBA')) exit('Access Denied.');

class User
{
    private $uid = 0;
    private $username = '';
    private $password = '';
    private $email = '';
    private $regdate = 0;
    private $activated = 0;
    private $enabled = 0;
    
    public function User($username = '', $password = '')
    {
        if ($username == '')
        {
            if (isset($_SESSION['uid']) && $_SESSION['uid'] != 0)
            {
                $this->uid = $_SESSION['uid'];
                $this->username = $_SESSION['username'];
                $this->password = $_SESSION['password'];
                $this->email = $_SESSION['email'];
                $this->regdate = $_SESSION['regdate'];
                $this->activated = $_SESSION['activated'];
                $this->enabled = $_SESSION['enabled'];
            }
        }
    	else $this->Login($username, $password);
    }
    
    public function Login($username, $password)
    {
        try
		{
			if (CheckAuthenticationAvailability())
	    	{
	    		global $db;
	    		$password = md5($password);
	    		$result = $db->query("SELECT * FROM {$db->tablepre}" . IMBA_AUTH_TABLE . " WHERE username = '{$username}' AND password = '{$password}'", 'result');
	    		if ($result)
	    		{
	    			$tmp = $db->fetch_array('result');
                    if (!$tmp) return;
 			    	$this->uid = $tmp['uid'];
				    $this->username = $tmp['username'];
				    $this->password = $tmp['password'];
				    $this->email = $tmp['email'];
				    $this->regdate = $tmp['regdate'];
				    $this->activated = $tmp['activated'];
				    $this->enabled = $tmp['enabled'];
                    $_SESSION['uid'] = $this->uid;
                    $_SESSION['username'] = $this->username;
                    $_SESSION['password'] = $this->password;
                    $_SESSION['email'] = $this->email;
                    $_SESSION['regdate'] = $this->regdate;
                    $_SESSION['activated'] = $this->activated;
                    $_SESSION['enabled'] = $this->enabled;
	    		}
	    	}
	    	else
	    	{
	    		throw new ImbaException(E_AUTH_DISABLED);
	    	}
		}
		catch (ImbaException $e)
		{
			$e->IncludeExceptionHandlerStop();
		}
    }
    
    public function Logout()
    {
        unset($_SESSION['uid']);
        unset($_SESSION['username']);
        unset($_SESSION['password']);
        unset($_SESSION['email']);
        unset($_SESSION['regdate']);
        unset($_SESSION['activated']);
        unset($_SESSION['enabled']);
        $this->uid = 0;
        $this->username = '';
        $this->password = '';
        $this->email = '';
        $this->regdate = 0;
        $this->activated = 0;
        $this->enabled = 0;
    }
    
    public function __get($name)
    {
    	switch ($name)
    	{
		    case 'uid': return $this->uid; break;
		    case 'username': return $this->username; break;
		    case 'password': return $this->password; break;
		    case 'email': return $this->email; break;
		    case 'regdate': return $this->regdate; break;
		    case 'activated': return $this->activated; break;
		    case 'enabled': return $this->enabled; break;
    	}
    }
}

?>